package com.ajie.service.impl;

import com.ajie.entity.Course;
import com.ajie.entity.New;
import com.ajie.mapper.CourseMapper;
import com.ajie.service.CourseService;
import com.ajie.utils.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Comparator;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class CourseServiceImpl implements CourseService {


    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private RedisUtil redisUtil;
    
    @Autowired
    JedisUtils jedisUtils ;
    
    @Override
    public Result findPage(@RequestBody QueryInfo queryInfo,String header) {
        log.info("该文件名是：CourseServiceImpl,该方法是：findPage");
        log.info("1.开始新闻咨询数据分页-->页码{}, -->页数{}, -->查询内容{}",queryInfo.getPageNumber(),queryInfo.getPageSize(),queryInfo.getQueryString());
        
        List<Course> result = courseMapper.findPage(queryInfo.getQueryString());
        
        result.forEach(course->{
            Long lookNum = jedisUtils.zscore("looks","course::"+course.getId()).longValue();
            Long likeNum = jedisUtils.zscore("likes","course::"+course.getId()).longValue();
            Long favouriteNum = jedisUtils.zscore("favourites","course::"+course.getId()).longValue();
            course.setLookNum(lookNum);
            course.setLikeNum(likeNum);
            course.setFavouriteNum(favouriteNum);
            boolean b = false;
            boolean b1 = false;
            boolean b2 = false;
            if (header != null){
                Set<Object> set = redisUtil.sGet("courseId::"+ course.getId().toString());
                //如果非空，遍、
                for (Object item2 : set){
                    String[] split = item2.toString().split("::");
                    String uid = split[2];
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if ("look".equals(split[0])){
                            b = true;
                        }
                        if ("like".equals(split[0])){
                            b1 = true;
                        }
                        if ("favourite".equals(split[0])){
                            b2 = true;
                        }
                    }
                }
            }
            course.setLook(b);
            course.setLike(b1);
            course.setFavourite(b2);
        });
        result.sort(Comparator.comparing(Course::getCreateTime).reversed());
        //4.把list进行分页
        Page page = new Page(queryInfo.getPageNumber(),queryInfo.getPageSize());
        // 获取list的大小
        int total = result.size();
    
        page.setTotal(total);
        int startIndex = (queryInfo.getPageNumber()-1) * queryInfo.getPageSize();
        int endIndex = Math.min(startIndex+queryInfo.getPageSize(),total);
        if (startIndex< total){
            page.addAll(result.subList(startIndex,endIndex));
        }
    
        PageInfo<Object> objectPageInfo = new PageInfo<>(page);
        log.info("查询的总条数是-->{}",total);
        log.info("分页列表-->{}",objectPageInfo.getList());
        return new PageResult(total, objectPageInfo.getList());
    }

    @Override
    public Result insert(Course course) {
        log.info("该文件名是：CourseServiceImpl,该方法是：insert");
        courseMapper.insert(course);
        redisUtil.zAdd("looks", "course::" + course.getId().toString(), 0);
        redisUtil.zAdd("likes","course::" + course.getId().toString(),0);
        redisUtil.zAdd("favourites","course::" + course.getId().toString(),0);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("添加新闻咨询数据成功");
    }

    @Override
    public Result delete(Long id) {
        log.info("该文件名是：CourseServiceImpl,该方法是：delete");
        courseMapper.delete(id);
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("删除新闻咨询数据成功");
    }

    @Override
    public Result update(Course course) {
        log.info("该文件名是：CourseServiceImpl,该方法是：update");
        courseMapper.update(course);
        // redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("修改新闻咨询数据成功");
    }
    
    
    /**
     * 获取详细课程
     * @param id
     * @return
     */
    @Override
    public Result getInfo(Long id) {
        log.info("该文件名是：CourseServiceImpl,该方法是：getInfo");
        return Result.success("获取信息成功",courseMapper.getInfo(id));
    }
    
    @Override
    public List<Course> getAll() {
        return courseMapper.getAll();
    }
    
    @Override
    public Result findPageCheck(QueryInfo queryInfo, String header) {
        log.info("该文件名是：CourseServiceImpl,该方法是：findPage");
        log.info("1.开始新闻咨询数据分页-->页码{}, -->页数{}, -->查询内容{}",queryInfo.getPageNumber(),queryInfo.getPageSize(),queryInfo.getQueryString());
    
        List<Course> result = courseMapper.findPageCheck(queryInfo.getQueryString());
    
        result.forEach(course->{
            Long lookNum = jedisUtils.zscore("looks","course::"+course.getId()).longValue();
            Long likeNum = jedisUtils.zscore("likes","course::"+course.getId()).longValue();
            Long favouriteNum = jedisUtils.zscore("favourites","course::"+course.getId()).longValue();
            course.setLookNum(lookNum);
            course.setLikeNum(likeNum);
            course.setFavouriteNum(favouriteNum);
            boolean b = false;
            boolean b1 = false;
            boolean b2 = false;
            if (header != null){
                Set<Object> set = redisUtil.sGet("courseId::"+ course.getId().toString());
                //如果非空，遍、
                for (Object item2 : set){
                    String[] split = item2.toString().split("::");
                    String uid = split[2];
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if ("look".equals(split[0])){
                            b = true;
                        }
                        if ("like".equals(split[0])){
                            b1 = true;
                        }
                        if ("favourite".equals(split[0])){
                            b2 = true;
                        }
                    }
                }
            }
            course.setLook(b);
            course.setLike(b1);
            course.setFavourite(b2);
        });
        result.sort(Comparator.comparing(Course::getCreateTime).reversed());
        //4.把list进行分页
        Page page = new Page(queryInfo.getPageNumber(),queryInfo.getPageSize());
        // 获取list的大小
        int total = result.size();
    
        page.setTotal(total);
        int startIndex = (queryInfo.getPageNumber()-1) * queryInfo.getPageSize();
        int endIndex = Math.min(startIndex+queryInfo.getPageSize(),total);
        if (startIndex< total){
            page.addAll(result.subList(startIndex,endIndex));
        }
    
        PageInfo<Object> objectPageInfo = new PageInfo<>(page);
        log.info("查询的总条数是-->{}",total);
        log.info("分页列表-->{}",objectPageInfo.getList());
        return new PageResult(total, objectPageInfo.getList());
    }
    
    @Override
    public Result updateStatus(Long id) {
        courseMapper.updateStatus(id);
        return Result.success("审核完成");
    }
    
    
}
